package com.plug.spring;

import com.plug.common.jdbc.DBConnectionManager;
import com.plug.common.jdbc.DBConnectionPool;
import com.plug.common.jdbc.JDBCInfoBean;
import com.plug.common.spring.utils.ApplicationContextHelper;
import com.plug.entity.PlugDatasource;
import com.plug.service.IPlugContrastService;
import com.plug.service.IPlugDatasourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;

/**
 * Created by admin on 2017/1/13.
 */
public class SpringTest {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private DBConnectionManager dbConnectionManager = DBConnectionManager.getInstance();

    public void insertTest(){
        long src = 50;
        long tgt = 100;
        IPlugContrastService plugContrastService = (IPlugContrastService) ApplicationContextHelper.getBean("plugContrastService");
        IPlugDatasourceService plugDatasourceService = (IPlugDatasourceService) ApplicationContextHelper.getBean("plugDatasourceService");
        PlugDatasource srcY = plugDatasourceService.findByID(src);
        Connection srcConn = null;
        srcConn = dbConnectionManager.getConnection(DBConnectionPool.createJDBCInfoBean(srcY));

        try {
            PreparedStatement ps = null;
            ps = srcConn.prepareStatement("insert into srctest(id,name) values(?,?)");
            for(int i = 0;i<2;i++){
                ps.setObject(1, 1);
                ps.setObject(2, "test");
                ps.addBatch();
            }
            ps.executeBatch();
            logger.debug("-------------------------------  insert into srctest(id,name) values(?,?)");
           // srcConn.commit();
            ps.close();
            ps = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (srcConn != null) {
                dbConnectionManager.freeConnection(srcY.getDsName(), srcConn);
            }
        }
    }




    public void insertTest2(){


        String sql  = "INSERT INTO `plug_server`.`proc` (`db`, `name`, `type`, `specific_name`, `sql_data_access`, `is_deterministic`, `definer`, `created`, `modified`, `sql_mode`, `comment`, `character_set_client`, `collation_connection`, `db_collation`) " +
                "VALUES ('dd', ?, 'PROCEDURE', 'rr', 'NO_SQL', 'YES', 'qq', '2017-01-14 13:39:01', '2017-01-14 13:39:02', ?, 'www', 'www', 'www', 'www');";


        long src = 1;

        //
        IPlugDatasourceService plugDatasourceService = (IPlugDatasourceService) ApplicationContextHelper.getBean("plugDatasourceService");

        IPlugContrastService plugContrastService = (IPlugContrastService) ApplicationContextHelper.getBean("plugContrastService");

        PlugDatasource srcY = plugDatasourceService.findByID(src);
        Connection srcConn = null;
        srcConn = dbConnectionManager.getConnection(DBConnectionPool.createJDBCInfoBean(srcY));

        try {
            PreparedStatement ps = null;
            ps = srcConn.prepareStatement(sql);
            for(int i = 0;i<2;i++){
                ps.setObject(1, "test"+i );
                ps.setObject(2, 1);
                ps.addBatch();
            }
            ps.executeBatch();

            logger.debug("------------------------------- zzb insert test data 测试 ");
            ps.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (srcConn != null) {
                dbConnectionManager.freeConnection(srcY.getDsName(), srcConn);
            }
        }

    }





}


